<template>
    <div v-loading="loading" class="app-container">
        <!-- 选择门诊和急诊的选项开始 -->
        <el-card style="margin-bottom:5px;text-align:center">
            <el-radio-group v-model="schedulingType" @change="schedulingTypeChange">
                <el-radio-button
                        v-for="dict in schedulingTypeOptions"
                        :key="dict.dictValue"
                        :label="dict.dictValue"
                        :value="dict.dictValue"
                >
                    {{ dict.dictLabel }}
                </el-radio-button>
            </el-radio-group>
        </el-card>
        <!-- 选择门诊和急诊的选项结束 -->

        <!-- 下面的整体卡片开始 -->
        <el-row :gutter="5">
            <el-col :span="7">
                <!-- 左边的患者信息开始 -->
                <el-card>
                    <el-form ref="form" :model="patientAllObj.patientObj"  :inline="true" label-width="88px">
                        <el-form-item label="患者姓名" prop="name">
                            <el-input
                                    v-model="patientAllObj.patientObj.name"
                                    placeholder="请输入患者姓名"
                                    :disabled="true"
                                    style="width:100%"
                                    size="small"
                            >
                                <el-button slot="append" icon="el-icon-user-solid" @click="viewRegistration" />
                            </el-input>
                        </el-form-item>
                        <el-form-item label="身份证号" prop="idCard">
                            <el-input
                                    v-model="patientAllObj.patientObj.idCard"
                                    placeholder="请输入身份证号"
                                    :disabled="true"
                                    style="width:100%"
                                    size="small"
                            />
                        </el-form-item>
                        <el-form-item label="患者电话" prop="phone">
                            <el-input
                                    v-model="patientAllObj.patientObj.phone"
                                    placeholder="请输入患者电话"
                                    :disabled="true"
                                    style="width:100%"
                                    size="small"
                            />
                        </el-form-item>
                        <el-form-item label="性别" prop="sex">
                            <el-radio-group v-model="patientAllObj.patientObj.sex">
                                <el-radio
                                        v-for="d in sexOptions"
                                        :key="d.dictValue"
                                        :label="d.dictValue"
                                        :value="d.dictValue"
                                >{{ d.dictLabel }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="出生日期" prop="phone">
                            <el-input
                                    v-model="patientAllObj.patientObj.birthday"
                                    placeholder="请选择出生日期"
                                    :disabled="true"
                                    style="width:100%"
                                    size="small"
                            />
                        </el-form-item>
                        <el-form-item label="患者年龄" prop="age">
                            <el-input
                                    v-model="patientAllObj.patientObj.age"
                                    placeholder="请输入患者年龄"
                                    :disabled="true"
                                    style="width:100%"
                                    size="small"
                            />
                        </el-form-item>
                        <el-form-item label="患者地址" prop="address">
                            <el-input
                                    v-model="patientAllObj.patientObj.address"
                                    placeholder="请输入患者地址"
                                    :disabled="true"
                                    style="width:100%"
                                    size="small"
                            />
                        </el-form-item>
                        <el-form-item label="过敏信息" prop="allergyInfo">
                            <el-input
                                    v-model="patientAllObj.patientObj.allergyInfo"
                                    placeholder="请输入过敏信息"
                                    type="textarea"
                                    :autosize="{minRows:2,maxRows:4}"
                                    :disabled="true"
                                    maxlength="100"
                                    show-word-limit
                                    style="width:100%"
                                    size="small"
                            />
                        </el-form-item>
                    </el-form>
                    <el-tabs :stretch="true" type="card">
                        <el-tab-pane label="患者档案">
                            <div v-if=" patientAllObj.patientObj.isFinal==='1' ">
                                <div class="item">
                                    紧急联系人:<span v-text="patientAllObj.patientFileObj.emergencyContactName" />
                                </div>
                                <div class="item">
                                    紧急联系人电话:<span v-text="patientAllObj.patientFileObj.emergencyContactPhone" />
                                </div>
                                <div class="item">
                                    关系: <span v-text="patientAllObj.patientFileObj.emergencyContactRelation" />
                                </div>
                                <div class="item">
                                    左耳听力:<span v-text="patientAllObj.patientFileObj.leftEarHearing" />
                                </div>
                                <div class="item">
                                    右耳听力:<span v-text="patientAllObj.patientFileObj.rightEarHearing" />
                                </div>
                                <div class="item">
                                    左眼视力:<span v-text="patientAllObj.patientFileObj.leftVision" />
                                </div>
                                <div class="item">
                                    右眼视力:<span v-text="patientAllObj.patientFileObj.rightVision" />
                                </div>
                                <div class="item">
                                    身高:<span v-text="patientAllObj.patientFileObj.height" />
                                </div>
                                <div class="item">
                                    体重:<span v-text="patientAllObj.patientFileObj.weight" />
                                </div>
                                <div class="item">
                                    血型:<span v-text="patientAllObj.patientFileObj.bloodType" />
                                </div>
                                <div class="item">
                                    个人史:<span v-text="patientAllObj.patientFileObj.personalInfo" />
                                </div>
                                <div class="item">
                                    家族史:<span v-text="patientAllObj.patientFileObj.familyInfo" />
                                </div>
                                <div class="item">
                                    档案创建时间:<span v-text="patientAllObj.patientFileObj.createTime" />
                                </div>
                                <div class="item">
                                    档案更新时间:<span v-text="patientAllObj.patientFileObj.updateTime" />
                                </div>
                            </div>
                            <div v-else style="text-align:center">
                                暂无患者档案信息
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="历史病历">
                            <div v-if="patientAllObj.careHistoryObjList.length>0">
                                <el-collapse accordion>
                                    <el-collapse-item v-for="(item,index) in patientAllObj.careHistoryObjList" :key="index">
                                        <template slot="title">
                                            就诊部门:【{{ item.deptName }}】 就诊时间:{{ item.careTime }}
                                        </template>
                                        <div class="item">
                                            主诉:{{ item.caseTitle }}
                                        </div>
                                        <div class="item">
                                            发病日期:{{ item.caseDate }}
                                        </div>
                                        <div class="item">
                                            诊断信息:{{ item.caseResult }}
                                        </div>
                                        <div class="item">
                                            医生建议:{{ item.doctorTips }}
                                        </div>
                                        <div class="item">
                                            备注:{{ item.remark }}
                                        </div>
                                        <div class="item">
                                            接诊类型:{{ item.receiveType==='0'?'初诊':'复诊' }}
                                        </div>
                                        <div class="item">
                                            是否传染:{{ item.isContagious==='0'?'否':'是' }}
                                        </div>
                                    </el-collapse-item>
                                </el-collapse>
                            </div>
                            <div v-else style="text-align:center">
                                暂无患者病历信息
                            </div>
                        </el-tab-pane>
                    </el-tabs>
                </el-card>

                <!-- 左边的患者信息结束 -->
            </el-col>
            <el-col :span="17">
                <!-- 右边的病历，处方开始 -->
                <el-card>
                    <el-row :gutter="5">
                        <el-col :span="12">
                            挂号单号:
                            <span v-if="careHistory.registrationId===undefined" style="color:red">选择患者后显示</span>
                            <span v-else style="color:red">{{ careHistory.registrationId }}</span>
                            <br>
                            病历编号:
                            <span v-if="careHistory.chId===undefined" style="color:red">保存病例后显示</span>
                            <span v-else style="color:blue">{{ careHistory.chId }}</span>
                        </el-col>
                        <el-col :span="12" style="text-align:right">
                            <el-button type="primary" :disabled="careHistory.registrationId===undefined" icon="el-icon-check" @click="handleSaveCareHistory">保存病历</el-button>
                            <el-button type="danger" :disabled="careHistory.registrationId===undefined" icon="el-icon-finished" @click="handleVisitComplete">完成就诊</el-button>
                        </el-col>
                    </el-row>
                </el-card>
                <el-card style="margin-top:3px">
                    <el-tabs :v-model="careActiveName" type="card">
                        <el-tab-pane label="病历">
                            <!-- 病例表单开始 -->
                            <el-card>
                                <el-form ref="form" :model="careHistory" :inline="true" label-width="88px">
                                    <el-form-item label="发病日期" prop="caseDate">
                                        <el-date-picker
                                                v-model="caseDateObj"
                                                value-format="yyyy-MM-dd"
                                                size="small"
                                        />
                                    </el-form-item>
                                    <el-form-item label="接诊类型" prop="receiveType">
                                        <el-select
                                                v-model="careHistory.receiveType"
                                                placeholder="接诊类型"
                                                size="small"
                                        >
                                            <el-option
                                                    v-for="dict in receiveTypeOptions"
                                                    :key="dict.dictValue"
                                                    :label="dict.dictLabel"
                                                    :value="dict.dictValue"
                                            />
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="是否传染" prop="isContagious">
                                        <el-select
                                                v-model="careHistory.isContagious"
                                                placeholder="是否传染"
                                                size="small"
                                        >
                                            <el-option
                                                    v-for="dict in isContagiousOptions"
                                                    :key="dict.dictValue"
                                                    :label="dict.dictLabel"
                                                    :value="dict.dictValue"
                                            />
                                        </el-select>
                                    </el-form-item>
                                </el-form>
                                <el-form ref="form" :model="careHistory" label-width="88px">
                                    <el-form-item label="主诉" :inline="false" prop="caseTitle">
                                        <el-input
                                                v-model="careHistory.caseTitle"
                                                type="textarea"
                                                :autosize="{ minRows: 4, maxRows: 6}"
                                                placeholder="请输入主诉"
                                                style="width:100%"
                                                maxlength="200"
                                                show-word-limit
                                                size="small"
                                        />
                                    </el-form-item>
                                    <el-form-item label="诊断信息" :inline="false" prop="caseResult">
                                        <el-input
                                                v-model="careHistory.caseResult"
                                                type="textarea"
                                                :autosize="{ minRows: 4, maxRows: 6}"
                                                placeholder="请输入诊断信息"
                                                style="width:100%"
                                                maxlength="200"
                                                show-word-limit
                                                size="small"
                                        />
                                    </el-form-item>
                                    <el-form-item label="医生建议" :inline="false" prop="doctorTips">
                                        <el-input
                                                v-model="careHistory.doctorTips"
                                                type="textarea"
                                                :autosize="{ minRows: 4, maxRows: 6}"
                                                placeholder="请输入医生建议"
                                                style="width:100%"
                                                maxlength="200"
                                                show-word-limit
                                                size="small"
                                        />
                                    </el-form-item>
                                    <el-form-item label="备注" :inline="false" prop="remark">
                                        <el-input
                                                v-model="careHistory.remark"
                                                type="textarea"
                                                :autosize="{ minRows: 4, maxRows: 6}"
                                                placeholder="请输入备注"
                                                style="width:100%"
                                                maxlength="200"
                                                show-word-limit
                                                size="small"
                                        />
                                    </el-form-item>
                                </el-form>
                            </el-card>
                            <!-- 病例表单结束 -->
                        </el-tab-pane>
                        <el-tab-pane label="处方">
                            <!-- 处方详情开始 -->
                            <el-collapse v-if="careOrders.length>0" accordion>
                                <el-collapse-item v-for="(item,index) in careOrders" :key="index">
                                    <template slot="title">
                                        【{{ item.coType==='0'?'药用处方':'检查处方' }}】【{{ index+1 }}】【处方总额】:￥{{ item.allAmount }}
                                    </template>
                                    <el-table
                                            v-loading="loading"
                                            border
                                            :data="item.careOrderItemList"
                                    >
                                        <el-table-column label="序号" align="center" type="index" width="50" />
                                        <el-table-column :label="item.coType==='0'?'药品名称':'项目名称'" align="center" prop="itemName" />
                                        <el-table-column label="数量" align="center" prop="num" />
                                        <el-table-column label="单价(元)" align="center" prop="price" />
                                        <el-table-column label="金额(元)" align="center" prop="amount" />
                                        <el-table-column label="备注" align="center" prop="remark" />
                                        <el-table-column label="状态" align="center" prop="status" :formatter="orderDetailsStatusFormatter" />
                                        <el-table-column label="操作" align="center">
                                            <template slot-scope="scope">
                                                <el-button v-show="scope.row.status==='0'" type="text" icon="el-icon-delete" size="mini" @click="handleCareOrderItemDeleteByItemId(scope.row)">删除</el-button>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </el-collapse-item>
                            </el-collapse>
                            <!-- 处方详情结束 -->
                            <div style="margin:3px;text-align:left">
                                <el-button type="success" icon="el-icon-plus" :disabled="careHistory.chId===undefined" @click="handelAddMedicinesOrder">添加药用处方</el-button>
                                <el-button type="success" icon="el-icon-plus" :disabled="careHistory.chId===undefined" @click="handelAddCheckItemOrder">添加检查处方</el-button>
                            </div>
                        </el-tab-pane>
                    </el-tabs>
                </el-card>
                <!-- 右边的病历，处方结束 -->
            </el-col>
        </el-row>
        <!-- 下面的整体卡片结束 -->

        <!-- 弹出选择患者的层开始 -->
        <el-dialog
                title="请选择挂号患者"
                :visible.sync="openRegistration"
                width="1000px"
                center
                :close-on-click-modal="false"
                append-to-body
        >
            <el-tabs v-model="activeName" :stretch="true" type="card" @tab-click="handleRegistrationTabClick">
                <el-tab-pane label="待就诊列表" name="t1">
                    <el-table v-loading="tableLoading" border :data="toBeSeenRegistration">
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-form label-position="right" inline class="demo-table-expand">
                                    <el-form-item label="挂号ID">
                                        <span>{{ scope.row.registrationId }}</span>
                                    </el-form-item>
                                    <el-form-item label="患者ID">
                                        <span>{{ scope.row.patientId }}</span>
                                    </el-form-item>
                                    <el-form-item label="挂号员">
                                        <span>{{ scope.row.createBy }}</span>
                                    </el-form-item>
                                    <el-form-item label="挂号时间">
                                        <span>{{ scope.row.createTime }}</span>
                                    </el-form-item>
                                </el-form>
                            </template>
                        </el-table-column>
                        <el-table-column label="患者姓名" align="center" prop="patientName" />
                        <el-table-column label="流水编号" align="center" prop="registrationNumber" />
                        <el-table-column label="挂号类型" align="center" prop="schedulingType" :formatter="schedulingTypeFormatter" />
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="success" icon="el-icon-check" size="mini" @click="handleVisit(scope.row)">接诊</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="就诊中列表" name="t2">
                    <el-table v-loading="tableLoading" border :data="visitingRegistration">
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-form label-position="right" inline class="demo-table-expand">
                                    <el-form-item label="挂号ID">
                                        <span>{{ scope.row.registrationId }}</span>
                                    </el-form-item>
                                    <el-form-item label="患者ID">
                                        <span>{{ scope.row.patientId }}</span>
                                    </el-form-item>
                                    <el-form-item label="挂号员">
                                        <span>{{ scope.row.createBy }}</span>
                                    </el-form-item>
                                    <el-form-item label="挂号时间">
                                        <span>{{ scope.row.createTime }}</span>
                                    </el-form-item>
                                </el-form>
                            </template>
                        </el-table-column>
                        <el-table-column label="患者姓名" align="center" prop="patientName" />
                        <el-table-column label="流水编号" align="center" prop="registrationNumber" />
                        <el-table-column label="挂号类型" align="center" prop="schedulingType" :formatter="schedulingTypeFormatter" />
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="success" icon="el-icon-check" size="mini" @click="handleLoading(scope.row)">载入</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="就诊完成列表" name="t3">
                    <el-table v-loading="tableLoading" border :data="visitCompletedRegistration">
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-form label-position="right" inline class="demo-table-expand">
                                    <el-form-item label="挂号ID">
                                        <span>{{ scope.row.registrationId }}</span>
                                    </el-form-item>
                                    <el-form-item label="患者ID">
                                        <span>{{ scope.row.patientId }}</span>
                                    </el-form-item>
                                    <el-form-item label="挂号员">
                                        <span>{{ scope.row.createBy }}</span>
                                    </el-form-item>
                                    <el-form-item label="挂号时间">
                                        <span>{{ scope.row.createTime }}</span>
                                    </el-form-item>
                                </el-form>
                            </template>
                        </el-table-column>
                        <el-table-column label="患者姓名" align="center" prop="patientName" />
                        <el-table-column label="流水编号" align="center" prop="registrationNumber" />
                        <el-table-column label="挂号类型" align="center" prop="schedulingType" :formatter="schedulingTypeFormatter" />
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="success" icon="el-icon-check" size="mini" @click="handleLoading(scope.row)">载入</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>
        </el-dialog>
        <!-- 弹出选择患者的层结束 -->

        <!-- 添加药用处方和检查出方的弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="openAddOrderItem"
                width="1000px"
                center
                :close-on-click-modal="false"
                append-to-body
        >
            <div style="margin:3px;text-align:right">
                【{{ submitCareOrder.careOrder.coType==='0'?'药用':'检查' }}】处方总额:￥<span style="color:red">{{ submitCareOrder.careOrder.allAmount }}</span>
                <el-button style="margin-left:15px" type="success" icon="el-icon-plus" :disabled="submitCareOrder.careOrderItems.length===0" @click="handleSaveOrderItem">确定添加</el-button>
            </div>
            <el-table
                    border
                    :data="submitCareOrder.careOrderItems"
                    :row-class-name="tableRowClassName"
            >
                <el-table-column label="序号" align="center" type="index" width="50" />
                <el-table-column :label="submitCareOrder.careOrder.coType==='0'?'药品名称':'项目名称'" align="center" prop="itemName" />
                <el-table-column label="数量" width="180px" align="center" prop="num">
                    <template slot-scope="scope">
                        <el-input-number
                                v-model="scope.row.num"
                                size="small"
                                @change="handleCareOrderItemNumChange(scope.row)"
                                :max="scope.row.stock"
                                :min="1"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="单价(元)" align="center" prop="price" />
                <el-table-column label="金额(元)" align="center" prop="amount" />
                <el-table-column label="备注" align="center" prop="remark">
                    <template slot-scope="scope">
                        <el-input
                                v-model="scope.row.remark"
                                size="small"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-delete" size="mini" @click="handleCareOrderItemDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="primary" size="mini" style="width:100%;margin-top:2px" @click="handleOpenAddOrderItemDrawer">添加【{{ submitCareOrder.careOrder.coType==='0'?'药品':'检查' }}】项 </el-button>
        </el-dialog>
        <!-- 添加药用处方和检查处方的弹出层结束 -->

        <!-- 药品列表的抽屉开始 -->
        <el-drawer
                :visible.sync="openDrawerMedicines"
                direction="rtl"
                append-to-body
        >
            <h2 align="center">药品列表</h2>
            <el-form ref="queryItemForm" :model="queryItemFormParams" label-width="68px">
                <el-form-item label="关键字" prop="keywords">
                    <el-input
                            v-model="queryItemFormParams.keywords"
                            placeholder="请输入关键字"
                            clearable
                            size="small"
                            style="width:180px"
                    />
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleMedicinesFormQuery">查询</el-button>
                    <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetItemFormQuery">重置</el-button>
                </el-form-item>

                <el-table
                        v-loading="drawerLoading"
                        height="600px"
                        border
                        :data="tableItemList"
                        @selection-change="handleMedicinesSelectionChange"
                >
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="药品ID" align="center" prop="medicinesId" />
                    <el-table-column label="药品名称" align="center" prop="medicinesName" />
                    <el-table-column label="关键字" align="center" prop="keywords" />
                    <el-table-column label="库存量" align="center" prop="medicinesStockNum" />
                    <el-table-column label="单位" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.conversion }}/ {{ scope.row.unit }}
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 数据表格结束 -->
                <!-- 分页控件开始 -->
                <el-pagination
                        v-show="total>0"
                        :current-page="queryItemFormParams.pageNum"
                        :page-sizes="[5, 10, 20, 30]"
                        :page-size="queryItemFormParams.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                        @size-change="handleMedicinesSizeChange"
                        @current-change="handleMedicinesCurrentChange"
                />
                <el-form-item>
                    <div style="text-align:center">
                        <el-button type="primary" icon="el-icon-check" size="mini" @click="hanldeAddCareItem">添加并关闭</el-button>
                    </div>
                </el-form-item>
            </el-form>
            <!-- 分页控件结束 -->
        </el-drawer>
        <!-- 药品列表的抽屉结束 -->

        <!-- 检查项目的抽屉开始 -->
        <el-drawer
                :visible.sync="openDrawerCheckItem"
                direction="rtl"
                append-to-body
        >
            <h2 align="center">检查项目列表</h2>
            <el-form ref="queryItemForm" :model="queryItemFormParams" label-width="68px">
                <el-form-item label="关键字" prop="keywords">
                    <el-input
                            v-model="queryItemFormParams.keywords"
                            placeholder="请输入关键字"
                            clearable
                            size="small"
                            style="width:180px"
                    />
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleCheckItemFormQuery">查询</el-button>
                    <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetItemFormQuery">重置</el-button>
                </el-form-item>

                <el-table
                        v-loading="drawerLoading"
                        height="600px"
                        border
                        :data="tableItemList"
                        @selection-change="handleCheckItemSelectionChange"
                >
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="项目ID" align="center" prop="checkItemId" />
                    <el-table-column label="项目名称" align="center" prop="checkItemName" />
                    <el-table-column label="关键字" align="center" prop="keywords" />
                    <el-table-column label="项目单价" align="center" prop="unitPrice" />
                    <el-table-column label="单位" align="center" prop="unit" />
                </el-table>
                <!-- 数据表格结束 -->
                <!-- 分页控件开始 -->
                <el-pagination
                        v-show="total>0"
                        :current-page="queryItemFormParams.pageNum"
                        :page-sizes="[5, 10, 20, 30]"
                        :page-size="queryItemFormParams.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                        @size-change="handleCheckItemSizeChange"
                        @current-change="handleCheckItemCurrentChange"
                />
                <el-form-item>
                    <div style="text-align:center">
                        <el-button type="primary" icon="el-icon-check" size="mini" @click="hanldeAddCareItem">添加并关闭</el-button>
                    </div>
                </el-form-item>
            </el-form>

        </el-drawer>
        <!-- 检查项目的抽屉结束 -->
    </div>
</template>

<script>
    export default {
        data() {
            return {
                // 总的遮罩层
                loading: false,
                // 表格的遮罩层
                tableLoading: false,
                // 门诊急诊的默认值
                schedulingType: '1',
                // 就诊类型的字典数据
                schedulingTypeOptions: [],
                // 排班类型的字典数据
                subsectionTypeOptions: [],
                // 性别数据字典
                sexOptions: [],
                // 接诊类型字典
                receiveTypeOptions: [],
                // 是否传染字典
                isContagiousOptions: [],
                // 处方详情的状态字典数据
                orderDetailsStatusOptions: [],
                // 是否打开选择挂号患者的弹层
                openRegistration: false,
                // 当前选中的挂号选项卡的
                activeName: 't1',
                // 右边的病例和处方的选项卡
                careActiveName: 'c1',
                // 待就诊数据
                toBeSeenRegistration: [],
                // 就诊中的数据
                visitingRegistration: [],
                // 就诊完成的数据
                visitCompletedRegistration: [],
                // 发病日期
                caseDateObj: new Date(),
                // 左边患者数据
                patientAllObj: {
                    patientObj: {},
                    //患者档案
                    patientFileObj: {},
                    //历史病历
                    careHistoryObjList: []
                },
                pat:{
                    patientId:undefined
                },
                // 提交到数据库的对象
                careHistory: {
                    // 当前就诊中的挂号单ID
                    registrationId: undefined,
                    chId: undefined,
                    caseDate: undefined,
                    receiveType: '0',
                    isContagious: '0',
                    caseTitle: undefined,
                    caseResult: undefined,
                    doctorTips: undefined,
                    remark: undefined,
                    patientId: undefined,
                    patientName: undefined
                },
                // 存放处方及详情数据
                careOrders: [],
                // 最后要提交到的处方及详情
                submitCareOrder: {
                    careOrder: {
                        allAmount: 0.00,
                        patientId: undefined,
                        patientName: undefined,
                        coType: '0' // 默认为药用处方
                    },
                    careOrderItems: []
                },
                // 弹出层的标题
                title: '',
                // 是否打开药品和检查项的添加的弹出层
                openAddOrderItem: false,
                // 是否打开药品列表的抽屉
                openDrawerMedicines: false,
                // 是否打开检查项目列表的抽屉
                openDrawerCheckItem: false,
                // 药品和检查项目抽屉的查询条件
                queryItemFormParams: {
                    pageNum: 1,
                    pageSize: 10,
                    keywords: undefined
                },
                // 药品或检查项目表格的数据
                tableItemList: [],
                // 抽屉里面数据的总条数
                total: 0,
                // 抽屉数据加载的遮罩
                drawerLoading: false,
                // 抽屉里面选中的数据
                selectItemList: [],
                initRegId:''
            }
        },

        created() {
            //加载挂号类型 急诊 门诊
            this.initschedulingType();
            this.initSex();

            this.initChuanRan()
            this.initChuFu()
            this.initorderDetailsStatus()
        },
        methods: {
            //加载是否传染
            initChuanRan(){
                this.$http.get("system/api/dict/data/findByType/his_receive_type").then(result => {
                    this.receiveTypeOptions = result.data.data;
                })
            },
            //接诊类型（出诊 复诊）
            initChuFu(){
                this.$http.get("system/api/dict/data/findByType/his_contagious_status").then(result => {
                    this.isContagiousOptions = result.data.data;
                })
            },

            // 加载挂号类型 门诊急诊
            initschedulingType() {
                this.$http.get("system/api/dict/data/findByType/his_scheduling_type").then(result => {
                    this.schedulingTypeOptions = result.data.data;
                })
            },
            // 解析 加载挂号类型
            schedulingTypeFormatter(row) {
                let v = row.schedulingType;
                return this.formatDict2(this.schedulingTypeOptions, v);
            },
            //初始化性别字典
            initSex(){
                this.$http.get("/system/api/dict/data/findByType/sys_user_sex").then(result=>{
                    this.sexOptions=result.data.data;
                })
            },

            //药与处方状态
            initorderDetailsStatus(){
                this.$http.get("/system/api/dict/data/findByType/his_order_details_status").then(result=>{
                    this.orderDetailsStatusOptions=result.data.data;
                })
            },

            //
            sexFormatter(row){
                let sex = row.sex;
                return this.formatDict2(this.sexOptions,sex);
            },
            // 根据出生年月计算年龄
            calculateAgeFormatter(row) {
                return this.getAge(row.birthday.substring(0, 10))
            },

            // 门诊急诊切换事件
            schedulingTypeChange(value) {
                this.schedulingType = value
            },
            // 打开选择挂号患者的弹出层
            viewRegistration() {
                this.activeName = 't1'
                this.openRegistration = true
                this.queryToBeSeenRegistration()
            },
            // 挂号患者弹出层的选项卡change事件
            handleRegistrationTabClick(tab, event) {
                if (tab.name === 't1') {
                    this.queryToBeSeenRegistration()
                } else if (tab.name === 't2') {
                    this.queryVisitingRegistration()
                } else if (tab.name === 't3') {
                    this.queryVisitCompletedRegistration()
                }
            },
            // 查询待就诊的数据
            queryToBeSeenRegistration() {
                this.$http.get("/doctor/registration/queryToBeSeenRegistration/"+this.schedulingType).then(result=>{
                    this.toBeSeenRegistration=result.data.data;
                })
            },
            // 查询就诊中的数据
            queryVisitingRegistration() {
                this.$http.get("/doctor/registration/queryVisitingRegistration/"+this.schedulingType).then(result=>{
                    this.visitingRegistration=result.data.data;
                })
            },
            // 查询就诊完成的数据
            queryVisitCompletedRegistration() {
                this.$http.get("/doctor/registration/queryVisitCompletedRegistration/"+this.schedulingType).then(result=>{
                    this.visitCompletedRegistration=result.data.data;
                })
            },

            // 翻译挂号类型
            schedulingTypeFormatter(row) {
                return this.selectDictLabel(this.schedulingTypeOptions, row.schedulingType)
            },
            // 翻译处方详情状态
            orderDetailsStatusFormatter(row) {
                return this.selectDictLabel(this.orderDetailsStatusOptions, row.status)
            },
            // 接诊
            handleVisit(row) {
                this.careHistory.registrationId = row.registrationId
                this.initRegId=row.registrationId;
                 const patientId = row.patientId
                this.$http.post("/doctor/registration/handleVisit/"+row.registrationId).then(result=>{
                    this.$message.success("接诊成功")
                    this.queryToBeSeenRegistration();
                    this.openRegistration = false;

                    this.$http.get("/doctor/patient/patientAllObject/"+row.patientId).then(result=>{
                        //alert(JSON.stringify(result.data.data))
                        //alert(row.patientId)
                        this.patientAllObj.patientObj = result.data.data.patientObj
                        this.pat=result.data.data.patientObj
                        //this.patientAllObj.patientObj.age = this.getAge(result.data.data.birthday);
                        //alert(JSON.stringify(this.patientAllObj.patientObj))
                        this.patientAllObj.patientFileObj = result.data.data.patientFileObj
                        this.patientAllObj.careHistoryObjList = result.data.data.careHistoryObjList
                        this.$message.success("查询信息成功")
                    })


                })

            },
            // 载入
            handleLoading(row) {
                this.careHistory.registrationId = row.registrationId
                const patientId = row.patientId
                this.$http.get("/doctor/patient/patientAllObject/"+patientId).then(result=>{
                    //赋值之前先清空上一个患者的数据
                    this.patientAllObj.patientObj={};
                    this.patientAllObj.patientFileObj={};
                    this.patientAllObj.careHistoryObjList=[];


                    this.patientAllObj.patientObj = result.data.data.patientObj

                    this.patientAllObj.patientFileObj = result.data.data.patientFileObj
                    this.patientAllObj.careHistoryObjList = result.data.data.careHistoryObjList
                    //alert(JSON.stringify(this.patientAllObj.careHistoryObjList))

                    this.openRegistration = false;

                    this.$message.success("载入信息成功")
                })

                this.getCareHistoryByRegId(row.registrationId)
            },
            // 根据挂号单ID查询对应的病历信息
            getCareHistoryByRegId() {
                this.$http.post("/doctor/carehistory/getCareHistoryByRegId/"+this.careHistory.registrationId).then(result => {

                    if(result.data.data != null){
                        this.careHistory = result.data.data[0]
                        this.careHistory.registrationId=result.data.data[0].regId

                        //如果有病历id(chId)查询处方信息
                        this.getCareOrdersByChId(result.data.chId)
                    }else {
                        this.resetCareHistoryAndCareOrders(regId)
                    }



                })
            },

            // 根据病历ID查询处方信息
            getCareOrdersByChId() {

                this.$http.post("/doctor/carehorderitem/queryCareOrdersByChId/"+this.careHistory.chId).then(result=>{
                    this.careOrders = result.data.data;
                    //alert(JSON.stringify(this.careOrders))
                 })
            },
            // 重置右边的病历和处方详情数据
            resetCareHistoryAndCareOrders(registrationId) {
                this.careHistory = {
                    // 当前就诊中的挂号单ID
                    registrationId: registrationId,
                    chId: undefined,
                    caseDate: undefined,
                    caseDateObj: new Date(),
                    receiveType: '0',
                    isContagious: '0',
                    caseTitle: undefined,
                    caseResult: undefined,
                    doctorTips: undefined,
                    remark: undefined,
                    patientId: undefined,
                    patientName: undefined
                }
                this.careOrders = []
            },
            // 保存病历
            handleSaveCareHistory() {
                // 封装数据
                 this.careHistory.caseDate = this.moment(this.caseDateObj).format('YYYY-MM-DD')
                 this.careHistory.patientId = this.patientAllObj.patientObj.patientId
                 this.careHistory.patientName = this.patientAllObj.patientObj.name
                //拼接
                this.$http.post("/doctor/carehistory/handleSaveCareHistory/"+this.careHistory.registrationId,this.careHistory).then(result=>{

                    this.careHistory.chId=result.data.data
                    this.$message.success("保存病历成功")
                })
            },

            // 打开添加药用处方的弹出层
            handelAddMedicinesOrder() {
                if (!this.careHistory.registrationId) {
                    this.$message.warning('请选择挂号患者')
                    return
                }
                if (!this.careHistory.chId) {
                    this.$message.warning('请先保存病历')
                    return
                }
                this.submitCareOrder.careOrder.coType = '0'
                this.title = '添加【药用】处方'
                this.openAddOrderItem = true
                this.submitCareOrder.careOrderItems = []
            },
            // 打开添加检查处方的弹出层
            handelAddCheckItemOrder() {
                if (!this.careHistory.registrationId) {
                    this.$message.warning('请选择挂号患者')
                    return
                }
                if (!this.careHistory.chId) {
                    this.$message.warning('请先保存病历')
                    return
                }
                this.submitCareOrder.careOrder.coType = '1'
                this.title = '添加【检查】处方'
                this.openAddOrderItem = true
                this.submitCareOrder.careOrderItems = []
            },
            // 打开药品或者检查项目的抽屉
            handleOpenAddOrderItemDrawer() {
                if (this.submitCareOrder.careOrder.coType === '0') {
                    // 打开药口列表抽屉
                    this.openDrawerMedicines = true
                    this.resetItemFormQuery()
                    this.getMedicinesList()
                } else {
                    // 打开检查项目的抽屉
                    this.openDrawerCheckItem = true
                    this.resetItemFormQuery()
                    this.getCheckItemList()
                }
            },

            // 加载药品数据
            getMedicinesList() {
                this.$http.post("/stock/api/med/semed/"+this.queryItemFormParams.pageNum+"/"+this.queryItemFormParams.pageSize,this.queryItemFormParams).then(result=>{
                    this.tableItemList = result.data.data.records;
                    this.total=result.data.data.total;

                })
            },
            // 数据表格的多选择框选择时触发
            handleMedicinesSelectionChange(selection) {
                this.selectItemList = selection
            },
            // 分页pageSize变化时触发
            handleMedicinesSizeChange(val) {
                this.queryItemFormParams.pageSize = val
                // 重新查询
                this.getMedicinesList()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleMedicinesCurrentChange(val) {
                this.queryItemFormParams.pageNum = val
                // 重新查询
                this.getMedicinesList()
            },
            // 搜索药品的方法
            handleMedicinesFormQuery() {
                this.queryItemFormParams.pageNum = 1
                this.getMedicinesList()
            },
            // 加载检查数据
            getCheckItemList() {
                //system/check-item/selectCheckItem
                //check/checkItem/selectCheckItem
                this.$http.post("/system/check-item/selectCheckItem/"+this.queryItemFormParams.pageNum+"/"+this.queryItemFormParams.pageSize,this.queryItemFormParams).then(result=>{
                    this.tableItemList =result.data.data.records;
                    this.total=result.data.data.total;
                })
            },
            // 数据表格的多选择框选择时触发
            handleCheckItemSelectionChange(selection) {
                this.selectItemList = selection
            },
            // 分页pageSize变化时触发
            handleCheckItemSizeChange(val) {
                this.queryItemFormParams.pageSize = val
                // 重新查询
                this.getCheckItemList()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCheckItemCurrentChange(val) {
                this.queryItemFormParams.pageNum = val
                // 重新查询
                this.getCheckItemList()
            },
            // 搜索检查项目的方法
            handleCheckItemFormQuery() {

                this.queryItemFormParams.pageNum = 1
                this.getCheckItemList()
            },
            // 重置抽屉的查询条件
            resetItemFormQuery() {
                this.queryItemFormParams = {
                    pageNum: 1,
                    pageSize: 10,
                    keywords: undefined
                }
                if (this.submitCareOrder.careOrder.coType === '0') {
                    this.getMedicinesList()
                } else {
                    this.getCheckItemList()
                }
            },
            // 点击药品或检查项目抽屉页面的添加并关闭按钮
            hanldeAddCareItem() {
                const coType = this.submitCareOrder.careOrder.coType
                if (this.selectItemList.length === 0) {
                    this.msgError('请选择【' + (coType === '0' ? '药品' : '检查项目') + '】')
                    return
                }
                if (coType === '0') { // 药品
                    this.selectItemList.filter(item => {
                        const obj = {
                            itemRefId: item.medicinesId,
                            itemName: item.medicinesName,
                            itemType: coType,
                            num: 1,
                            price: item.prescriptionPrice,
                            amount: 1 * item.prescriptionPrice,
                            remark: '请按说明服用',
                            stock:item.medicinesStockNum,
                        }
                        let flag = false// 默认里面没有加
                        this.submitCareOrder.careOrderItems.filter(i => {
                            if (i.itemRefId === obj.itemRefId) {
                                i.num = i.num + 1
                                flag = true// 说明之前加过
                            }
                        })
                        if (!flag) {
                            this.submitCareOrder.careOrderItems.push(obj)
                        }
                        this.openDrawerMedicines = false
                    })
                } else { // 检查项目
                    this.selectItemList.filter(item => {
                        const obj = {
                            itemRefId: item.checkItemId,
                            itemName: item.checkItemName,
                            itemType: coType,
                            num: 1,
                            price: item.unitPrice,
                            amount: 1 * item.unitPrice,
                            remark: '按要求检查'
                        }
                        let flag = false// 默认里面没有加
                        this.submitCareOrder.careOrderItems.filter(i => {
                            if (i.itemRefId === obj.itemRefId) {
                                i.num = i.num + 1
                                flag = true// 说明之前加过
                            }
                        })
                        if (!flag) {
                            this.submitCareOrder.careOrderItems.push(obj)
                        }
                        this.openDrawerCheckItem = false
                    })
                }
                // 计算总价
                this.computeOrderItemAllAmount()
            },
            // 删除弹出层里面的详情
            handleCareOrderItemDelete(row) {
                this.submitCareOrder.careOrderItems.splice(row.index, 1)
                this.computeOrderItemAllAmount()
            },
            // 把弹出层的表格的数据加上index
            tableRowClassName({ row, rowIndex }) {
                row.index = rowIndex
            },
            // 计算当前处方详情的总价
            computeOrderItemAllAmount() {
                this.submitCareOrder.careOrder.allAmount = 0.00
                this.submitCareOrder.careOrderItems.filter(item => {
                    this.submitCareOrder.careOrder.allAmount +=
                        (item.num * item.price)
                })
            },
            // 监听药品或检查项目弹出层的数量的变化
            handleCareOrderItemNumChange(row) {
                row.amount = row.num * row.price
                this.computeOrderItemAllAmount()
            },
            // 保存处方单信息
            handleSaveOrderItem() {

                if (this.submitCareOrder.careOrderItems.length === 0) {
                    this.msgError('添加处方详情')
                    return
                }
                this.submitCareOrder.careOrder.patientId = this.careHistory.patientId
                this.submitCareOrder.careOrder.patientName = this.careHistory.patientName
                this.submitCareOrder.careOrder.chId = this.careHistory.chId
                console.log(this.submitCareOrder)
                this.$http.post("/doctor/carehorderitem/saveCareOrderItem",this.submitCareOrder).then(result=>{
                    this.$message.success("添加成功")
                    this.loading = false
                    // 刷新处方列表
                    this.getCareOrdersByChId(this.careHistory.chId)
                    // 关闭当前弹出层
                    this.openAddOrderItem = false
                }).catch(() => {
                    this.msgError('保存失败')
                    this.loading = false
                })
            },
            // 根据详情ID删除数据库里面的详情【只能删除未支付的】
            handleCareOrderItemDeleteByItemId(row) {
                //alert(JSON.stringify(row))
                const itemId = row.itemId
                const itemName = row.itemName
                const amount=row.amount
                const tx = this
                tx.$confirm('是否确定删除【' + itemName + '】这条详情, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //alert(itemId)
                    this.$http.post("/doctor/carehorderitem/deleteCareOrderItemById/"+itemId+"/"+amount).then(result=>{
                        this.$message.success('删除成功')
                        // 刷新处方列表
                        this.getCareOrdersByChId(this.careHistory.chId)
                    }).catch(() => {
                        this.$message.error("删除失败")
                    })
                }).catch(() => {
                    tx.$message.warning('删除已取消')
                    tx.loading = false
                })
            },
            // 完成就诊
            handleVisitComplete() {
                //const regId = this.careHistory.regId
                const registrationId = this.careHistory.registrationId
                const patientName = this.careHistory.patientName
                const tx = this
                tx.$confirm('是否确定完成【' + patientName + '】的就诊, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // this.$http.post('/doctor/registration/wcjz/'+this.registrationId){
                    //
                    // }
                    //visitComplete(regId).then(res => {
                    this.$http.post('/doctor/registration/wcjz/'+this.careHistory.registrationId).then(result=>{


                        //this.$http.post()
                        //this.msgSuccess('操作成功')
                        this.$message.success("保存病历成功")
                        // 重置所有数据
                        this.resetAllData()
                    }).catch(() => {
                        this.msgError('操作失败')
                    })
                }).catch(() => {
                    tx.msgError('操作已取消')
                    tx.loading = false
                })
            },
            // 重置所有数据
            resetAllData() {
                this.patientAllObj = {
                    patientObj: {},
                    patientFileObj: {},
                    careHistoryObjList: []
                }
                this.careHistory = {
                    // 当前就诊中的挂号单ID
                    regId: undefined,
                    chId: undefined,
                    caseDate: undefined,
                    receiveType: '0',
                    isContagious: '0',
                    caseTitle: undefined,
                    caseResult: undefined,
                    doctorTips: undefined,
                    remark: undefined,
                    patientId: undefined,
                    patientName: undefined
                }
                this.careOrders = []
                this.submitCareOrder = {
                    careOrder: {
                        allAmount: 0.00,
                        patientId: undefined,
                        patientName: undefined,
                        coType: '0' // 默认为药用处方
                    },
                    careOrderItems: []
                }
            }
        }
    }
</script>
<style scoped>
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
    .item{
        font-size: 14px;
        margin-bottom: 8px;
    }
</style>
